package br.com.mtg.movethegarbage.screens;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import br.com.mtg.movethegarbage.framework.Game;
import br.com.mtg.movethegarbage.framework.GameTouchEvent;
import br.com.mtg.movethegarbage.framework.Input;
import br.com.mtg.movethegarbage.framework.Screen;
import br.com.mtg.movethegarbage.game.Assets;
import br.com.mtg.movethegarbage.game.R;
import br.com.mtg.movethegarbage.graphics.Camera2D;
import br.com.mtg.movethegarbage.graphics.SpriteManager;
import br.com.mtg.movethegarbage.impl.Button;

public class InfoScreen extends Screen
{
	Button back, blog;
	SpriteManager spriteManager;
	Camera2D cam;

	public InfoScreen(Game _game)
	{
		super(_game);

		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		multiplier = game.getGraphics().getMultiplier();
		spriteManager = new SpriteManager(game.getGraphics(), 1);
		cam = new Camera2D(game.getGraphics());

		back = new Button(10, 10, 40 * multiplier, 40 * multiplier);
		back.setButtonSprite(Assets.buttonBack);
		Assets.font.setScale(1.0F * multiplier);
		float stringWidth = Assets.font.getTextLength(game.getStringFromResource(R.string.blog));
		blog = new Button(width - (stringWidth + 40), 10, game.getStringFromResource(R.string.blog), 1.0F * multiplier, Assets.buttonBackgroundRect);
	}

	@Override
	public void update(long delta)
	{
		Input input = game.getInput();
		synchronized (input)
		{
			for (GameTouchEvent e : input.getTouchEvents())
			{
				back.handleEvent(e, game);
				blog.handleEvent(e, game);
			}
			input.getTouchEvents().clear();

			if (back.wasPressed() || input.wasBackPressed())
				game.setScreen(new MainMenuScreen(game));
			else if (blog.wasPressed())
			{
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://movethegarbage.blogspot.com.br/"));
				((Activity) game).startActivity(browserIntent);
			}
		}

	}

	@Override
	public void present(long delta)
	{
		spriteManager.beginDraw(Assets.tex_backgroundScreenRedMain);
		spriteManager.drawSprite(0, 0, width, height, Assets.background);
		spriteManager.endDraw();

		Assets.font.setScale(0.7F * multiplier);
		Assets.font.printAt(game.getStringFromResource(R.string.info), 10, height - (Assets.font.getTextHeight() + 10));

		Assets.font.printAt(game.getStringFromResource(R.string.name1), 10, height - (2 * Assets.font.getTextHeight() + 20));
		Assets.font.printAt(game.getStringFromResource(R.string.name2), 10, height - (3 * Assets.font.getTextHeight() + 20));
		Assets.font.printAt(game.getStringFromResource(R.string.name3), 10, height - (4 * Assets.font.getTextHeight() + 20));
		Assets.font.printAt(game.getStringFromResource(R.string.name4), 10, height - (5 * Assets.font.getTextHeight() + 20));
		Assets.font.printAt(game.getStringFromResource(R.string.name5), 10, height - (6 * Assets.font.getTextHeight() + 20));

		back.draw(game.getGraphics());
		blog.draw(game.getGraphics());
	}

	@Override
	public void pause()
	{
	}

	@Override
	public void resume()
	{
		cam.setViewAndMatrices();

		game.getGraphics().initialize();

		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		multiplier = game.getGraphics().getMultiplier();
	}

	@Override
	public void dispose()
	{
	}

}
